package com.codeoss.framework.controller;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.codeoss.framework.response.DocReportPageResponse;
import com.codeoss.framework.service.IDocReportPageService;

@Controller
@RequestMapping("docreportpage")
public class DocReportPageController {
	@Autowired
	private IDocReportPageService docReportPageService;
	
	@RequestMapping(value = "init", method = RequestMethod.GET)
	@ResponseBody
	public DocReportPageResponse  init(@RequestParam("reportCode")String reportCode,@RequestParam("recordId")Long recordId){
		DocReportPageResponse docReportPageResponse=docReportPageService.init(reportCode, recordId);
		return docReportPageResponse;
	}
	
	@RequestMapping(value = "dataExport", method = RequestMethod.GET)
	@ResponseBody
	public void  dataExport(@RequestParam("reportCode")String reportCode,@RequestParam("recordId")Long recordId,@RequestParam("format")String format,
			HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)throws Exception{
		httpServletResponse.reset();
		try {
			httpServletResponse.addHeader("Content-Disposition","attachment;filename="+new String(("单据打印."+format).getBytes(),"ISO-8859-1"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		OutputStream out=httpServletResponse.getOutputStream();
	    docReportPageService.dataExport(reportCode, recordId, format, out);
	}
}
